/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-19
*/
package com.rzico.basics.controller.admin;

import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.MemberCategory;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户分类控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-19
 */
@Api(description = "客户分类接口")
@RestController
@RequestMapping("/admin/memberCategory")
public class MemberCategoryAdminController extends BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MemberCategoryService memberCategoryService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询客户分类
     *
     * @return
    */
    @ApiOperation("分页查询客户分类")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分类ID", dataType = "Long", paramType = "query")})
    public CommResult<MemberCategory> list(Long id) {
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        if (null != id){
            params.put("treePath", id);
        }
        params.put("sortField", "tree_path");
        params.put("sortType", "asc");
        List<MemberCategory> list = memberCategoryService.selectTree(params);
        return CommResult.success(list);
    }

    /**
     * 查询单条客户分类
     *
     * @return
    */
    @ApiOperation("查询单条客户分类")
    @GetMapping("/find/{id}")
    public CommResult<MemberCategory> find(@PathVariable String id) {
        MemberCategory result = memberCategoryService.findById(id);
        Map<String,Object> data = new HashMap<>();
        data.put("memberCategory",result);
        return CommResult.success(result);

    }

    /**
     * 保存客户分类
     *
     * @param memberCategory
     * @return
     */
    @ApiOperation("保存客户分类")
    @PostMapping("/save")
    public CommResult<MemberCategory> save(@RequestBody MemberCategory memberCategory) {
        Assert.notNull(memberCategory,"获取数据失败");
        Assert.notNull(memberCategory.getName(), "菜单名称为空");
        logger.info("会员保存传参: [{}]", memberCategory.toString());

        Enterprise enterprise = enterpriseService.getCurrent();
        memberCategory.setEnterpriseId(enterprise.getId());
        if (null == memberCategory.getIsShow()){
            memberCategory.setIsShow(true);
        }
        int affectCount = memberCategoryService.insertUseGeneratedKeys(memberCategory);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        MemberCategory result = memberCategoryService.findById(memberCategory.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除客户分类
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除客户分类", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除客户分类,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<MemberCategory> del(@PathVariable String ids) {

        memberCategoryService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新客户分类
     *
     * @param memberCategory
     * @return
     */
    @Log(desc = "更新客户分类", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新客户分类")
    @PostMapping("/update")
    public CommResult<MemberCategory> update(@RequestBody MemberCategory memberCategory) {

        int affectCount = memberCategoryService.updateByPrimaryKeySelective(memberCategory);
        if (affectCount <= 0){
            return CommResult.error();
        }
        MemberCategory result = memberCategoryService.findById(memberCategory.getId());
        return CommResult.success(result);
    }

}
